home *** CD-ROM | disk | FTP | other *** search
- Path: news.voicenet.com!usenet
- From: deaton@cygnus.rsabbs.com
- Newsgroups: comp.lang.c
- Subject: Bizarre error with structures, please help!
- Date: 16 Mar 1996 05:44:24 GMT
- Organization: Voicenet - Internet Access - (215)674-9290
- Message-ID: <4idkfo$1el@news.voicenet.com>
- NNTP-Posting-Host: d02.rsabbs.com
- X-Newsreader: SPRY News 3.03 (SPRY, Inc.)
-
-
- I'm writing a short program to init the default values for a data file. All my routine does is assign hardcoded (ack!) values to the
- variables the structure array, and then save it to a random access file via fwrite(). And just to test the code, I open the file with an fread()
- and print the values to the screen with a "for" loop. EVERYTHING looks fine, but in the 13th element, when the variable wtype is set to
- 13 (same value as array reference) the values from that structure on go crazy (except the empty ones at the end). I'm going nuts, I've already
- spent over ten hours trying to find the bug in this simple thing... ANY help is REALLY appreciated! :) Thanks in advance!
-
- Here's the code:
-
- #define WEP_REF 30 //Max number of weapon slots in weapon ref array.
-
- typedef struct
- {
- char type[20]; //Weapon name.
- int cost; //Cost of weapon in Gold.
- int damhi; //High range of damage for the weapon.
- int damlo; //Low range of damage for the weapon.
- int wtype; //Numerical weapon reference.
- int missle; //Is the weapon a missle weapon? (1 or 0)
- char size; //'S'mall or 'L'arge. No shield with large.
- int actual; //Actual damage done directly to a unit.
- int special; //Special case damage. If 1, actual damage ammount is dealt
- //to the armor, and remainder is done to the unit.
- }weaponS;
-
- //****** PROGRAM TO MAKE DEFAULT WEAPON FILE *********
-
- #include "stdio.h"
- #include "conio.h"
- #include "ctype.h"
- #include "D:\doug's\orcs\struct.h"
-
- static weaponS w[WEP_REF];
-
- main()
- {//MAIN
-
- register int count;
- FILE * fptr;
- printf("\n\n\n\n\n\n\Populating Structure.");
-
- strcpy(w[0].type,"Dagger");
- w[0].cost=0;
- w[0].size='s';
- w[0].wtype=0;
- w[0].damhi=4;
- w[0].damlo=1;
- w[0].actual=1;
- w[0].special=0;
- w[0].missle=0;
-
- putch('.');
-
- strcpy(w[1].type,"Sabre");
- w[1].cost=0;
- w[1].size='s';
- w[1].wtype=1;
- w[1].damhi=5;
- w[1].damlo=2;
- w[1].actual=1;
- w[1].special=0;
- w[1].missle=0;
-
- putch('.');
-
- strcpy(w[2].type,"Short Sword");
- w[2].cost=0;
- w[2].size='s';
- w[2].wtype=2;
- w[2].damhi=6;
- w[2].damlo=1;
- w[2].actual=1;
- w[2].special=0;
- w[2].missle=0;
-
- putch('.');
-
- strcpy(w[3].type,"Club");
- w[3].cost=0;
- w[3].size='s';
- w[3].wtype=3;
- w[3].damhi=6;
- w[3].damlo=1;
- w[3].actual=1;
- w[3].special=0;
- w[3].missle=0;
-
- putch('.');
-
- strcpy(w[4].type,"Hammer");
- w[4].cost=0;
- w[4].size='s';
- w[4].wtype=4;
- w[4].damhi=6;
- w[4].damlo=1;
- w[4].actual=1;
- w[4].special=0;
- w[4].missle=0;
-
- putch('.');
-
- strcpy(w[5].type,"Long Knife");
- w[5].cost=0;
- w[5].size='s';
- w[5].wtype=5;
- w[5].damhi=6;
- w[5].damlo=1;
- w[5].actual=1;
- w[5].special=0;
- w[5].missle=0;
-
- putch('.');
-
- strcpy(w[6].type,"Broadsword");
- w[6].cost=0;
- w[6].size='s';
- w[6].wtype=6;
- w[6].damhi=8;
- w[6].damlo=1;
- w[6].actual=1;
- w[6].special=0;
- w[6].missle=0;
-
- putch('.');
-
- strcpy(w[7].type,"Scimitar");
- w[7].cost=1;
- w[7].size='s';
- w[7].wtype=7;
- w[7].damhi=8;
- w[7].damlo=1;
- w[7].actual=1;
- w[7].special=0;
- w[7].missle=0;
-
- putch('.');
-
- strcpy(w[8].type,"Mace");
- w[8].cost=0;
- w[8].size='s';
- w[8].wtype=8;
- w[8].damhi=8;
- w[8].damlo=1;
- w[8].actual=1;
- w[8].special=0;
- w[8].missle=0;
-
- putch('.');
-
- strcpy(w[9].type,"Long Sword");
- w[9].cost=0;
- w[9].size='s';
- w[9].wtype=9;
- w[9].damhi=10;
- w[9].damlo=1;
- w[9].actual=1;
- w[9].special=0;
- w[9].missle=0;
-
- putch('.');
-
- strcpy(w[10].type,"Morning Star");
- w[10].cost=0;
- w[10].size='s';
- w[10].wtype=10;
- w[10].damhi=8;
- w[10].damlo=2;
- w[10].actual=2;
- w[10].special=0;
- w[10].missle=0;
-
- putch('.');
-
- strcpy(w[11].type,"Battle Axe");
- w[11].cost=0;
- w[11].size='l';
- w[11].wtype=11;
- w[11].damhi=8;
- w[11].damlo=2;
- w[11].actual=2;
- w[11].special=0;
- w[11].missle=0;
-
- putch('.');
-
- strcpy(w[12].type,"Spear");
- w[12].cost=1;
- w[12].size='l';
- w[12].wtype=12;
- w[12].damhi=12;
- w[12].damlo=2;
- w[12].actual=2;
- w[12].special=0;
- w[12].missle=0;
-
- putch('.');
-
- strcpy(w[13].type,"Warhammer");
- w[13].cost=0;
- w[13].size='l';
- w[13].wtype=13; //<-----THIS IS THE PROBLEM LINE--WHEN IT'S SET TO 13,
- w[13].damhi=12; //THE WHOLE THING GOES HAYWIRE!!
- w[13].damlo=2;
- w[13].actual=2;
- w[13].special=0;
- w[13].missle=0;
-
- putch('.');
-
- strcpy(w[14].type,"Boar Spear");
- w[14].cost=2;
- w[14].size='l';
- w[14].wtype=14;
- w[14].damhi=12;
- w[14].damlo=3;
- w[14].actual=3;
- w[14].special=0;
- w[14].missle=0;
-
- putch('.');
-
- strcpy(w[15].type,"Halberd");
- w[15].cost=0;
- w[15].size='l';
- w[15].wtype=15;
- w[15].damhi=12;
- w[15].damlo=3;
- w[15].actual=3;
- w[15].special=0;
- w[15].missle=0;
-
- putch('.');
-
- strcpy(w[16].type,"Bow");
- w[16].cost=0;
- w[16].size='l';
- w[16].wtype=16;
- w[16].damhi=6;
- w[16].damlo=1;
- w[16].actual=1;
- w[16].special=0;
- w[16].missle=1;
-
- putch('.');
-
- strcpy(w[17].type,"Crossbow");
- w[17].cost=3;
- w[17].size='l';
- w[17].wtype=17;
- w[17].damhi=8;
- w[17].damlo=2;
- w[17].actual=2;
- w[17].special=1;
- w[17].missle=1;
-
- putch('.');
-
- printf("\nOpening file for writing...");
- fptr=fopen("weapon.dat","w+"); //Open file for writing. If file
- //exists, erase and create a new one.
- printf("\nWriting Structure to Disk...");
- fwrite(w,sizeof(w),1,fptr);
-
- printf("\nClosing file...");
- fclose(fptr);
-
- printf("\nSuccess!");
-
- return(0);
-
- }//MAIN
-
- ==================================TEST PROGRAM
- #include "stdio.h"
- #include "conio.h"
- #include "d:\doug's\orcs\struct.h"
-
- main()
- {
- register int count;
- FILE * fptr;
- static weaponS w[WEP_REF];
- int junk;
-
- fptr=fopen("weapon.dat","r+");
- fread(w,sizeof(w),1,fptr);
- fclose(fptr);
-
- //====================TEST-PRINT THE RESULTS=========================
- clrscr();
-
- puts("Printing out default Weapon specifications...");
- for(count=0;count<WEP_REF;count++)
- {
- printf("\n\nName:%s",w[count].type);
- printf("\nCost:%d",w[count].cost);
- printf("\nwtype:%d",w[count].wtype);
- printf("\nDamhi:%d",w[count].damhi);
- printf("\nDamlo:%d",w[count].damlo);
- printf("\nActual:%d",w[count].actual);
- printf("\nSpecial:%d",w[count].special);
- printf("\nMissle:%d",w[count].missle);
- printf("\nSize:%c\n\n",w[count].size);
- junk=getch();
- }
-
- return(0);
- }//main()
-
-
-
-
-